import random
from .settings import *
from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
from utils.logging import get_logger
logger = get_logger()


#发送验证码函数
def get_code():
    code_str = ''
    for i in range(4):
        code_str +=str(random.randint(0,9))
    return code_str
#发送短信函数
def send_sms(phone,code):
    ssender = SmsSingleSender(APPPID, APPKEY)
    params = [code, '1']  # 当模板没有参数时，`params = []`
    try:
        result: object = ssender.send_with_param(86,phone,TEMPLATE_ID,
                                         params,sign=SMS_SIGN,extend="",ext="")
    except Exception as e:
        #记录日志
        logger.error('%s手机号，发送短信失败，错误信息为%s'%(phone,str(e)))
    if result['result'] != 0:
        logger.warning('%s手机号，发送短信失败,失败原因是%s' %(phone,result['errmsg']))
        return False
    return True





